<?php
/*
* analyzer类负责生成每个URL对应的页面分析对象，但是这些分析对象只与URL有关，只是页面分析工具，并不与数据库直接耦合。
* 根据每个网站的不同情况，有每个网站的分析类
*/
class SiteAnalyzer
{
    const DEFAULT_CHARSET = 'UTF-8';
    private static $suffix = array('.com.cn','.gov.cn','.org.cn','.net.cn','.edu.cn','.ac.cn','.bj.cn','.sh.cn','.tj.cn','.cq.cn','.he.cn','.sx.cn','.nm.cn','.ln.cn','.jl.cn','.hl.cn','.js.cn','.zj.cn','.ah.cn','.fj.cn','.jx.cn','.sd.cn','.ha.cn','.hb.cn','.hn.cn','.gd.cn','.gx.cn','.hi.cn','.sc.cn','.gz.cn','.yn.cn','.xz.cn','.sn.cn','.gs.cn','.qh.cn','.nx.cn','.xj.cn','.tw.cn','.hk.cn','.mo.cn','.co.at','.or.at','.com.hr','.co.ee','.eu.com','.com.de','.de.com','.com.gr','.gr.com','.co.gl','.com.gl','.co.gg','.co.hu','.hu.com','.ac.im','.co.im','.com.im','.net.im','.org.im','.co.il','.org.il','.co.je','.com.mk','.com.mt','.net.mt','.org.mt','.co.nl','.co.no','.no.com','.biz.pl','.com.pl','.info.pl','.net.pl','.org.pl','.com.pt','.com.ro','.com.ru','.net.ru','.ru.com','.co.rs','.in.rs','.com.es','.nom.es','.org.es','.se.com','.se.net','.biz.tr','.com.tr','.info.tr','.tv.tr','.com.ua','.co.uk','.gb.com','.org.uk','.uk.com','.uk.net','.gb.net',); 
    protected $url = '';
    public $site = null;
    public $item_id = null;
    public $item_info = null;
    public $page_content = null;
    public static function analyzerFactory($url)
    {
        //目前是按照域名来分不同的处理类
        $parse = parse_url($url);
        $domain = self::getTopdomain($parse['host']);
        //此处不进行任何输入验证，调用domain相关子类的instance方法。
        $domain = ucwords(str_replace('.', ' ', strtolower($domain)));
        $classname = implode('_', array_reverse(explode(' ',$domain))).'Analyzer';
        if(!class_exists($classname,false) && !file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.$classname.'.php'))
        {
            $classname = 'DefaultAnalyzer';
        }
        return new $classname($url);
    }

    /*
    * 此类不能实例化
    */
    private function __constructer()
    {
    }

    public function get_refund_url()
    {
        return $this->url;
    }


    public function getProductInfo()
    {
        if(!isset($this->product_info))
        {
            $this->product_info = array();
            $this->product_info['site_name']    = $this->get_site_name();
            $this->product_info['item_id']    = $this->get_item_id();
            $this->product_info['item_title'] = $this->get_item_title();
            $this->product_info['item_intro'] = $this->get_item_intro();
            $this->product_info['item_price'] = $this->get_item_price();
            $this->product_info['item_imgs']  = $this->get_item_imgs();
        }
        return $this->product_info;
    }

    public function get_site_charset()
    {
        return self::DEFAULT_CHARSET;
    }

    public function get_site_name()
    {
        return $this->site->name;
    }

    public function get_item_id()
    {
        return null;
    }

    public function get_item_title()
    {
        return null;
    }

    public function get_item_intro()
    {
        return null;
    }

    public function get_item_price()
    {
        return null;
    }

    public function get_item_imgs()
    {
        return null;
    }

    public static function getTopdomain($host)
    {
        $host = strtolower($host); 
        $dots = substr_count($host,'.'); 
        if(!$dots) 
            return false; 
        if($dots == 1) 
        { 
            return $host; 
        } 
        else 
        { 
            $hostlen = strlen($host); 
            foreach (self::$suffix as $sf) 
            { 
                if(substr($host,-strlen($sf)) == $sf) 
                { 
                    $prefix = substr($host,0,$hostlen - strlen($sf)); 
                    if(strrpos($prefix,'.') === false) 
                    { 
                        return $host; 
                    }
                    else 
                    { 
                        return substr($prefix,strrpos($prefix,'.')+1).$sf; 
                    } 
                } 
            } 
            return substr($host,strrpos(substr($host,0,strrpos($host,'.')),'.')+1); 
        }
    }

    /*
    * 获取页面内容。
    */
    public function get_page_content()
    {
        if(!isset($this->page_content))
        {
            $page = MyController::get_url($this->url);
            if($page['code'] == '200')
            {
                $this->page_content = $page['content'];
                if($this->get_site_charset() != self::DEFAULT_CHARSET)
                {
                    //如果网站不是UTF8编码的，那么进行转码，保证返回时编码的统一。优先使用mb_convert_encoding进行转换，iconv有时候不稳定
                    if(function_exists('mb_convert_encoding'))
                    {
                        $this->page_content = mb_convert_encoding($this->page_content, self::DEFAULT_CHARSET,$this->get_site_charset());
                    }
                    else
                    {
                        $this->page_content = @iconv($this->get_site_charset(), self::DEFAULT_CHARSET.'//IGNORE', $this->page_content);
                    }
                }
            }
            else
            {
                $this->page_content = '';
            }
        }
        return $this->page_content;
    }

    public static function urlhash($url)
    {
        return sprintf('%u',crc32($url));
    }

    public function get_url_hash()
    {
        return self::urlhash($this->url);
    }
}